package com.gzhryc.pay.adaptation.alipay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.gzhryc.common.NumberTools;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.pay.adaptation.weixin.msg.native_pay.WXPayNativeRefundResponse;
import com.gzhryc.pay.models.IPayOrder;
import com.gzhryc.pay.models.IRefundOrder;

import java.io.Serializable;
import java.util.Properties;

public class AlipayNativeService implements Serializable {

    private static final long serialVersionUID = 1L;

	static Logger log = Logger.getLogger(AlipayNativeService.class);

    DefaultAlipayConfig config;

    public AlipayNativeService(){}

    public AlipayNativeService(Properties properties) {
        this.config = PropertyTools.build(properties).getObject(DefaultAlipayConfig.class);
    }

    public AlipayNativeService(DefaultAlipayConfig config) {
        this.config = config;
    }

    public AlipayTradePrecreateResponse applyNativeOrder(IPayOrder payOrder){
        AlipayTradePrecreateResponse response = null;
        try {
            DefaultAlipayClient httpClient = AliHttpClient.getHttpClient(config);

            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
            request.setNotifyUrl(config.getPayNotifyUrl());
            JSONObject bizContent = new JSONObject();
            bizContent.put("out_trade_no", payOrder.getTradeNo());
            bizContent.put("total_amount", NumberTools.changeMoney(payOrder.getPayMoney()));
            bizContent.put("subject", payOrder.getTitle());

            //// 商品明细信息，按需传入
            //JSONArray goodsDetail = new JSONArray();
            //JSONObject goods1 = new JSONObject();
            //goods1.put("goods_id", "goodsNo1");
            //goods1.put("goods_name", "子商品1");
            //goods1.put("quantity", 1);
            //goods1.put("price", 0.01);
            //goodsDetail.add(goods1);
            //bizContent.put("goods_detail", goodsDetail);

            //// 扩展信息，按需传入
            //JSONObject extendParams = new JSONObject();
            //extendParams.put("sys_service_provider_id", "2088511833207846");
            //bizContent.put("extend_params", extendParams);

            //// 结算信息，按需传入
            //JSONObject settleInfo = new JSONObject();
            //JSONArray settleDetailInfos = new JSONArray();
            //JSONObject settleDetail = new JSONObject();
            //settleDetail.put("trans_in_type", "defaultSettle");
            //settleDetail.put("amount", 0.01);
            //settleDetailInfos.add(settleDetail);
            //settleInfo.put("settle_detail_infos", settleDetailInfos);
            //bizContent.put("settle_info", settleInfo);

            //// 二级商户信息，按需传入
            //JSONObject subMerchant = new JSONObject();
            //subMerchant.put("merchant_id", "2088000603999128");
            //bizContent.put("sub_merchant", subMerchant);

            //// 业务参数信息，按需传入
            //JSONObject businessParams = new JSONObject();
            //businessParams.put("busi_params_key", "busiParamsValue");
            //bizContent.put("business_params", businessParams);

            //// 营销信息，按需传入
            //JSONObject promoParams = new JSONObject();
            //promoParams.put("promo_params_key", "promoParamsValue");
            //bizContent.put("promo_params", promoParams);

            request.setBizContent(bizContent.toString());
            response = httpClient.execute(request);
        } catch (AlipayApiException e) {
            log.error(e.getMessage(), e);
        } catch (LogicException e) {
            log.error(e.getMessage(), e);
        }
        return response;
    }

    public WXPayNativeRefundResponse applyNativeRefund(IPayOrder payOrder, IRefundOrder refundOrder){
        return null;
    }
}
